<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>卡片名称</span>
        <el-button type="primary" @click="dialogFormVisible = true">添加分类</el-button>
      </div>
      <el-table :data="tableData" style="width: 100%" stripe border>
        <el-table-column prop="id" label="ID" width="180"> </el-table-column>
        <el-table-column prop="cate_name" label="分类名称"> </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>

        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button size="mini" @click="bianji(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="dep(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加弹出框 -->
    <el-dialog title="添加分类" :visible.sync="dialogFormVisible" width="35%" :before-close="send">
      <el-form :model="adddata" :rules="rules" ref="formRef">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input autocomplete="off" v-model="adddata.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input autocomplete="off" v-model="adddata.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="send">取 消</el-button>
        <el-button type="primary" @click="adds">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 编辑弹出框 -->
    <el-dialog title="添加分类" :visible.sync="dialogForm" width="35%">
      <el-form ref="formRef2" :rules="rules" :model="editData">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input autocomplete="off" v-model="editData.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input autocomplete="off" v-model="editData.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogForm = false">取 消</el-button>
        <el-button type="primary" @click="updtaeaaa">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { booksAPI, addAPI, delAPI, updatedAPI } from '@/api/catagory'
export default {
  name: 'MyCate',
  data() {
    return {
      tableData: [],
      dialogFormVisible: false, // 控制添加的弹出框

      dialogForm: false, // 控制编辑的弹出框
      // 收集编辑的数据
      editData: [],

      adddata: {
        cate_name: '',
        cate_alias: ''
      },
      rules: {
        cate_name: [
          { required: true, message: '分类名称必填', trigger: 'blur' },
          { min: 1, max: 10, message: '长度1~10位', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '分类别名必填', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '只能是长度1~15位的字母数字组合',
            trigger: 'blur'
          }
        ]
      }
    }
  },

  methods: {
    // 获取分类
    async gets() {
      const { data: res } = await booksAPI()
      if (res.code === 0) {
        this.tableData = res.data
      }
    },

    // 添加分类的方法
    adds() {
      this.$refs.formRef.validate(async vaild => {
        if (!vaild) return
        const { data: res } = await addAPI(this.adddata)
        // console.log(res)

        if (res.code === 0) {
          this.$message.success(res.message) // 提示
          this.$refs.formRef.resetFields() // 重置表单
          this.gets() // 重新获取分类列表数据
          this.dialogFormVisible = false // 关闭弹框
        } else {
          this.$message.error(res.message)
        }
      })
    },

    // 取消添加的方法
    send() {
      this.dialogFormVisible = false
      this.$refs.formRef.resetFields()
    },

    // 删除按钮
    dep(id) {
      // console.log(id)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        type: 'warning',
        callback: async r => {
          if (r === 'confirm') {
            const { data: res } = await delAPI(id)
            if (res.code === 0) {
              this.$message.success(res.message)
              this.gets()
            } else {
              this.$message.error(res.message)
            }
          }
        }
      })

      // Promise对象形式
      // .then(() => {
      //   this.$message({
      //     type: 'success',
      //     message: '删除成功!'
      //   })
      // })
      // .catch(() => {
      //   this.$message({
      //     type: 'info',
      //     message: '已取消删除'
      //   })
      // })
    },

    // 编辑按钮
    bianji(row) {
      // console.log(row)
      this.dialogForm = true
      this.editData = { ...row }
    },

    // 更新方法
    updtaeaaa() {
      this.$refs.formRef2.validate(async vaild => {
        const { data: res } = await updatedAPI(this.editData)
        if (res.code === 0) {
          this.$message.success(res.message)
          this.gets()
          this.dialogForm = false
        } else {
          this.$message.error(res.message)
        }
      })
    }
  },

  created() {
    this.gets()
  }
}
</script>

<style scoped>
.clearfix {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
